<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/6/9
 * Time: 14:21
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Privateproduct extends Base
{
    /**
     * 产品首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $pp_name = input('pp_name');
            $pp_status = input('pp_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['pp_status'] = ['NEQ', 3];
            if(isset($pp_name) && $pp_name != '') $where['pp_name'] = ['like', "%$pp_name%"];
            if(isset($pp_status) && $pp_status != '') $where['pp_status'] = $pp_status;
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != '') $where['pp_time'] = ['between', [$start_time, $end_time]];
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == '') $where['pp_time'] = ['>', $start_time];
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != '') $where['pp_time'] = ['<', $end_time];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('private_product')
                ->join('up_private_setmeal', 'pp_ps_id = ps_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('private_product')
                ->join('up_private_setmeal', 'pp_ps_id = ps_id', 'LEFT')
                ->where($where)
                ->field('pp_id, pp_name, ps_name, pp_price, pp_spec, pp_image, pp_status, pp_time, pp_update_time')
                ->page($NowPage, $limits)
                ->order('pp_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加产品
     * @return mixed|\think\response\Json
     */
    public function productAdd(){
        if(Request::instance()->isPost()){
            $pp_ps_id = input('pp_ps_id');
            $pp_name = input('pp_name');
            $pp_price = input('pp_price');
            $pp_spec = input('pp_spec');
            $pp_image = input('pp_image');
            $pp_status = input('pp_status');
            
            $insert = [
                'pp_ps_id' => $pp_ps_id,
                'pp_name' => $pp_name,
                'pp_price' => $pp_price,
                'pp_spec' => $pp_spec,
                'pp_image' => $pp_image,
                'pp_status' => $pp_status,
                'pp_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_product')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $setmeal = Db::table('up_private_setmeal')->where('ps_status', 1)->field('ps_id, ps_name')->select();
        return $this->fetch('privateproduct/productadd', ['setmeal' => $setmeal]);
    }
    
    /**
     * 产品详情
     * @return mixed
     */
    public function productDetail(){
        $pp_id = input('pp_id');
        $product = Db::table('up_private_product')
            ->join('up_private_setmeal', 'pp_ps_id = ps_id', 'LEFT')
            ->where('pp_id', $pp_id)
            ->field('ps_name, pp_name, pp_price, pp_spec, pp_image, pp_status, pp_time')
            ->find();
        return $this->fetch('privateproduct/productdetail', ['product' => $product]);
    }
    
    /**
     * 编辑产品
     * @return mixed|\think\response\Json
     */
    public function productEdit(){
        $pp_id = input('pp_id');
        if(Request::instance()->isPost()){
            $pp_ps_id = input('pp_ps_id');
            $pp_name = input('pp_name');
            $pp_price = input('pp_price');
            $pp_spec = input('pp_spec');
            $pp_image = input('pp_image');
            $pp_status = input('pp_status');
    
            $update = [
                'pp_ps_id' => $pp_ps_id,
                'pp_name' => $pp_name,
                'pp_price' => $pp_price,
                'pp_spec' => $pp_spec,
//                'pp_image' => $pp_image,
                'pp_status' => $pp_status,
                'pp_update_time' => date('Y-m-d H:i:s')
            ];
            if($pp_image) $update['pp_image'] = $pp_image;
            $res = Db::table('up_private_product')->where('pp_id', $pp_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        // 产品
        $product = Db::table('up_private_product')
            ->where('pp_id', $pp_id)
            ->field('pp_ps_id, pp_name, pp_price, pp_spec, pp_image, pp_status')
            ->find();
        // 套餐
        $setmeal = Db::table('up_private_setmeal')->where('ps_status', 1)->field('ps_id, ps_name')->select();
        
        $this->assign([
            'pp_id' => $pp_id,
            'product' => $product,
            'setmeal' => $setmeal
        ]);
        return $this->fetch('privateproduct/productedit');
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function productDown(){
        $pp_id = input('id');
        $res = Db::table('up_private_product')->where('pp_id', $pp_id)->setField('pp_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function productOn(){
        $pp_id = input('id');
        $res = Db::table('up_private_product')->where('pp_id', $pp_id)->setField('pp_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除产品
     * @return \think\response\Json
     */
    public function productDel(){
        $pp_id = input('id');
        $res = Db::table('up_private_product')->where('pp_id', $pp_id)->setField('pp_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}